﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarCrash.Core.Model;
using CarCrash.Core.Services;

namespace CarCrash.Application.Drawing.entities.Forms
{
    public partial class TSForm : Form
    {
        private TS ts;

        public TSForm(TS ts)
        {
            InitializeComponent();
            this.ts = ts;
        }
        //переменные и списки для работы с бд
        IList<String> listBrands = null;
        IList<Vehicle> models;
        Vehicle selectVehicle;
        String susp = "";
        String carbody = "";

        //метод загрузки данных из бд.. 
        private void DataLoadFromBD()
        {
            VehicleService.Instance.OpenSession();
            listBrands = VehicleService.Instance.GetBrands();
            VehicleService.Instance.CloseSession();
            //в списки авто заносим все доступные бренды
            foreach (String s in listBrands)
            {
                this.comboBox1.Items.Add(s);
                //this.comboBox2.Items.Add(v.Model);
                this.comboBox3.Items.Add(s);
                ///this.comboBox4.Items.Add(v.Model);
                this.comboBox5.Items.Add(s);
                this.comboBox6.Items.Add(s);
                this.comboBox7.Items.Add(s);
                this.comboBox8.Items.Add(s);
            }

        }

        private void SetVehicleParametersToForm()
        {
            textBox4.Text = selectVehicle.Parameters.NumberOfAxes.ToString();
            textBox5.Text = selectVehicle.Parameters.Length.ToString();
            textBox6.Text = selectVehicle.Parameters.Width.ToString();
        }

        //логика работы списков
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = 0;
            models = new List<Vehicle>();
            models.Clear();
            VehicleService.Instance.OpenSession();
            models = VehicleService.Instance.GetAllByBrand(comboBox1.SelectedItem.ToString());
            VehicleService.Instance.CloseSession();
            foreach (Vehicle v in models)
            {
                comboBox2.Items.Add(v.Model);
                comboBox4.Items.Add(v.Model);
                i++;

            }
            if (comboBox2.SelectedItem != null)
            {
                VehicleService.Instance.OpenSession();
                selectVehicle = VehicleService.Instance.GetByModel(
                comboBox1.SelectedItem.ToString(),
                comboBox2.SelectedItem.ToString()
                );
                VehicleService.Instance.CloseSession();
                SetVehicleParametersToForm();
            }
            comboBox3.Text = comboBox1.SelectedItem.ToString();
            comboBox5.Text = comboBox1.SelectedItem.ToString();
            comboBox6.Text = comboBox1.SelectedItem.ToString();
            comboBox7.Text = comboBox1.SelectedItem.ToString();
            comboBox8.Text = comboBox1.SelectedItem.ToString();
            textBox1.Text = "В базе " + i + " моделей этой марки";
            textBox3.Text = "В базе " + i + " моделей этой марки";
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem != null)
            {
                VehicleService.Instance.OpenSession();
                selectVehicle = VehicleService.Instance.GetByModel(
                  comboBox1.SelectedItem.ToString(),
                comboBox2.SelectedItem.ToString()
               );
                VehicleService.Instance.CloseSession();
                SetVehicleParametersToForm();
                comboBox4.Text = comboBox2.SelectedItem.ToString();
                textBox2.Text = " Данные загружены";
                textBox8.Text = " Данные загружены";
            }
            else
            {
                textBox2.Text = "Выберите марку";
                textBox8.Text = "Выберите марку";
            }
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = 0;
            VehicleService.Instance.OpenSession();
            models = VehicleService.Instance.GetAllByBrand(
                comboBox1.SelectedItem.ToString());
            VehicleService.Instance.CloseSession();
            foreach (Vehicle v in models)
            {
                comboBox2.Items.Add(v.Model);
                comboBox4.Items.Add(v.Model);
                i++;
            }
            if (comboBox2.SelectedItem != null)
            {
                VehicleService.Instance.OpenSession();
                selectVehicle = VehicleService.Instance.GetByModel(
                    comboBox3.SelectedItem.ToString(),
                    comboBox2.SelectedItem.ToString()
                    );
                VehicleService.Instance.CloseSession();
                SetVehicleParametersToForm();
            }
            comboBox1.Text = comboBox1.SelectedItem.ToString();
            comboBox5.Text = comboBox1.SelectedItem.ToString();
            comboBox6.Text = comboBox1.SelectedItem.ToString();
            comboBox7.Text = comboBox1.SelectedItem.ToString();
            comboBox8.Text = comboBox1.SelectedItem.ToString();
            textBox1.Text = "В базе " + i + " моделей этой марки";
            textBox3.Text = "В базе " + i + " моделей этой марки";
        }

        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem != null)
            {
                VehicleService.Instance.OpenSession();
                selectVehicle = VehicleService.Instance.GetByModel(
                    comboBox1.SelectedItem.ToString(),
                    comboBox4.SelectedItem.ToString()
                    );
                VehicleService.Instance.CloseSession();
                SetVehicleParametersToForm();
                comboBox2.Text = comboBox4.SelectedItem.ToString();
                textBox2.Text = " Данные загружены";
                textBox8.Text = " Данные загружены";
            }
            else
            {
                textBox2.Text = "Выберите марку";
                textBox8.Text = "Выберите марку";
            }
        }

        private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = 0;
            VehicleService.Instance.OpenSession();
            models = VehicleService.Instance.GetAllByBrand(
                comboBox1.SelectedItem.ToString());
            VehicleService.Instance.CloseSession();
            foreach (Vehicle v in models)
            {
                comboBox2.Items.Add(v.Model);
                comboBox4.Items.Add(v.Model);
                i++;

            }
            if (comboBox2.SelectedItem != null)
            {
                VehicleService.Instance.OpenSession();
                selectVehicle = VehicleService.Instance.GetByModel(
                    comboBox5.SelectedItem.ToString(),
                    comboBox2.SelectedItem.ToString()
                    );
                VehicleService.Instance.CloseSession();
                SetVehicleParametersToForm();
            }
            comboBox1.Text = comboBox1.SelectedItem.ToString();
            comboBox3.Text = comboBox1.SelectedItem.ToString();
            comboBox6.Text = comboBox1.SelectedItem.ToString();
            comboBox7.Text = comboBox1.SelectedItem.ToString();
            comboBox8.Text = comboBox1.SelectedItem.ToString();
            textBox1.Text = "В базе " + i + " моделей этой марки";
            textBox3.Text = "В базе " + i + " моделей этой марки";
        }

        private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = 0;
            VehicleService.Instance.OpenSession();
            models = VehicleService.Instance.GetAllByBrand(
                comboBox1.SelectedItem.ToString());
            VehicleService.Instance.CloseSession();
            foreach (Vehicle v in models)
            {
                comboBox2.Items.Add(v.Model);
                comboBox4.Items.Add(v.Model);
                i++;

            }
            if (comboBox2.SelectedItem != null)
            {
                VehicleService.Instance.OpenSession();
                selectVehicle = VehicleService.Instance.GetByModel(
                    comboBox6.SelectedItem.ToString(),
                    comboBox2.SelectedItem.ToString()
                    );
                VehicleService.Instance.CloseSession();
                SetVehicleParametersToForm();
            }
            comboBox1.Text = comboBox1.SelectedItem.ToString();
            comboBox5.Text = comboBox1.SelectedItem.ToString();
            comboBox3.Text = comboBox1.SelectedItem.ToString();
            comboBox7.Text = comboBox1.SelectedItem.ToString();
            comboBox8.Text = comboBox1.SelectedItem.ToString();
            textBox1.Text = "В базе " + i + " моделей этой марки";
            textBox3.Text = "В базе " + i + " моделей этой марки";
        }

        private void comboBox7_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = 0;
            VehicleService.Instance.OpenSession();
            models = VehicleService.Instance.GetAllByBrand(
                comboBox1.SelectedItem.ToString());
            VehicleService.Instance.OpenSession();
            foreach (Vehicle v in models)
            {
                comboBox2.Items.Add(v.Model);
                comboBox4.Items.Add(v.Model);
                i++;

            }
            if (comboBox2.SelectedItem != null)
            {
                VehicleService.Instance.OpenSession();
                selectVehicle = VehicleService.Instance.GetByModel(
                    comboBox7.SelectedItem.ToString(),
                    comboBox2.SelectedItem.ToString()
                    );
                VehicleService.Instance.CloseSession();
                SetVehicleParametersToForm();
            }
            comboBox1.Text = comboBox1.SelectedItem.ToString();
            comboBox5.Text = comboBox1.SelectedItem.ToString();
            comboBox6.Text = comboBox1.SelectedItem.ToString();
            comboBox3.Text = comboBox1.SelectedItem.ToString();
            comboBox8.Text = comboBox1.SelectedItem.ToString();
            textBox1.Text = "В базе " + i + " моделей этой марки";
            textBox3.Text = "В базе " + i + " моделей этой марки";
        }

        private void comboBox8_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = 0;
            VehicleService.Instance.OpenSession();
            models = VehicleService.Instance.GetAllByBrand(
                comboBox1.SelectedItem.ToString());
            VehicleService.Instance.CloseSession();
            foreach (Vehicle v in models)
            {
                comboBox2.Items.Add(v.Model);
                comboBox4.Items.Add(v.Model);
                i++;

            }
            if (comboBox2.SelectedItem != null)
            {
                VehicleService.Instance.OpenSession();
                selectVehicle = VehicleService.Instance.GetByModel(
                    comboBox8.SelectedItem.ToString(),
                    comboBox2.SelectedItem.ToString()
                    );
                VehicleService.Instance.CloseSession();
                SetVehicleParametersToForm();
            }
            comboBox1.Text = comboBox1.SelectedItem.ToString();
            comboBox5.Text = comboBox1.SelectedItem.ToString();
            comboBox6.Text = comboBox1.SelectedItem.ToString();
            comboBox7.Text = comboBox1.SelectedItem.ToString();
            comboBox3.Text = comboBox1.SelectedItem.ToString();
            textBox1.Text = "В базе " + i + " моделей этой марки";
            textBox3.Text = "В базе " + i + " моделей этой марки";
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                textBox14.ReadOnly = false;
            }
            else
            {
                textBox14.Text = "0";
                textBox14.ReadOnly = true;
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
            {
                textBox30.ReadOnly = false;
            }
            else
            {
                textBox30.Text = "0";
                textBox30.ReadOnly = true;
            }
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked)
            {
                textBox31.ReadOnly = false;
            }
            else
            {
                textBox31.Text = "0";
                textBox31.ReadOnly = true;
            }
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox4.Checked)
            {
                textBox32.ReadOnly = false;
            }
            else
            {
                textBox32.Text = "0";
                textBox32.ReadOnly = true;
            }
        }

        private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox5.Checked)
            {
                textBox33.ReadOnly = false;
                textBox34.ReadOnly = false;
                textBox35.ReadOnly = false;
            }
            else
            {
                textBox33.Text = "0";
                textBox33.ReadOnly = true;
                textBox34.Text = "0";
                textBox34.ReadOnly = true;
                textBox35.Text = "0";
                textBox35.ReadOnly = true;
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            susp = "stiff";
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            susp = "normal";
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            susp = "soft";
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            carbody = "stiff";
        }

        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            carbody = "normal";
        }

        private void radioButton6_CheckedChanged(object sender, EventArgs e)
        {
            carbody = "soft";
        }

        private void TSForm_Load(object sender, EventArgs e)
        {
            textBox4.Text = "0";
            textBox5.Text = "0";
            textBox6.Text = "0";
            textBox14.Text = "0";
            textBox14.ReadOnly = true;
            textBox30.Text = "0";
            textBox30.ReadOnly = true;
            textBox31.Text = "0";
            textBox31.ReadOnly = true;
            textBox32.Text = "0";
            textBox32.ReadOnly = true;
            textBox33.Text = "0";
            textBox33.ReadOnly = true;
            textBox34.Text = "0";
            textBox34.ReadOnly = true;
            textBox35.Text = "0";
            textBox35.ReadOnly = true;
            DataLoadFromBD();
        }
        //кнопка закрытия формы
        private void Submit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //пересылка данных в переменную для отрисовки ТС
        //считывание числа осей ТС
        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            int numberofaxles;
            if (Int32.TryParse(textBox4.Text, out numberofaxles)) ts.NumberOfAxles = numberofaxles;
        }
        //считывание длины ТС
        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            double lenght;
            if (Double.TryParse(textBox5.Text, out lenght)) ts.Lenght = lenght;
        }
        //считывание ширины ТС
        private void textBox6_TextChanged(object sender, EventArgs e)
        {
            double width;
            if (Double.TryParse(textBox6.Text, out width)) ts.Width = width;
        }
    }
}
